package smecta.client.print.controller;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Callback;
import smecta.client.print.SpringbootJavafxApplication;
import smecta.client.print.util.TableUtil;

import java.io.IOException;

import static smecta.client.print.SpringbootJavafxApplication.applicationContext;

public class MainController {

	@FXML
	private AnchorPane anchorPaneCom;

	@FXML
	private Label welcomeLabel;

	@FXML
	private MenuItem exitMenuItem;

	@FXML
	private MenuItem settingsMenuItem;

	@FXML
	private MenuItem aboutMenuItem;

	@FXML
	private Button taskProcessingButton;

	@FXML
	private Button dataQueryButton;

	@FXML
	private Button systemSettingsButton;

	@FXML
	private Button labelPrintingButton;

	@FXML
	private Button helpButton;

    @FXML
    private MenuItem codeQueryMenuItem;
    @FXML
    private MenuItem dataAcqMenuItem;


	@FXML
	private MenuItem summaryQueryMenuItem;

	@FXML
	private MenuItem finishedProductQueryMenuItem;

	@FXML
	private MenuItem comprehensiveQueryMenuItem;
	@FXML
	private MenuItem initialSetupMenuItem;

	@FXML
	private MenuItem scaleSettingMenuItem;

	@FXML
	private MenuItem productionReportMenuItem;

	@FXML
	private MenuItem dailyProductionReportMenuItem;

	@FXML
	private MenuItem monthlyProductionReportMenuItem;

	@FXML
	private MenuItem splitCodePrintMenuItem;
	@FXML
	private Button closeButton;

	@FXML
	private MenuItem helpMenuItem;

	@FXML
	private void initialize() {
		Image image1 = new Image("icon/prescriptions.png");
		ImageView imageView1 = new ImageView(image1);
		imageView1.setFitWidth(20); // 设置图标的宽度
		imageView1.setFitHeight(20); // 设置图标的高度
		dataAcqButton.setGraphic(imageView1);

		Image image2 = new Image("icon/settings.png");
		ImageView imageView2 = new ImageView(image2);
		imageView2.setFitWidth(20); // 设置图标的宽度
		imageView2.setFitHeight(20); // 设置图标的高度
		productSettingsButton.setGraphic(imageView2);

		Image image3 = new Image("icon/grid.png");
		ImageView imageView3 = new ImageView(image3);
		imageView3.setFitWidth(20); // 设置图标的宽度
		imageView3.setFitHeight(20); // 设置图标的高度
		productionReportButton.setGraphic(imageView3);

		Image image4 = new Image("icon/rule.png");
		ImageView imageView4 = new ImageView(image4);
		imageView4.setFitWidth(20); // 设置图标的宽度
		imageView4.setFitHeight(20); // 设置图标的高度
		codeModificationButton.setGraphic(imageView4);

		Image image5 = new Image("icon/search.png");
		ImageView imageView5 = new ImageView(image5);
		imageView5.setFitWidth(20); // 设置图标的宽度
		imageView5.setFitHeight(20); // 设置图标的高度
		codeQueryButton.setGraphic(imageView5);

		Image image6 = new Image("icon/search_insights.png");
		ImageView imageView6 = new ImageView(image6);
		imageView6.setFitWidth(20); // 设置图标的宽度
		imageView6.setFitHeight(20); // 设置图标的高度
		summaryQueryButton.setGraphic(imageView6);

		Image image7 = new Image("icon/print.png");
		ImageView imageView7 = new ImageView(image7);
		imageView7.setFitWidth(20); // 设置图标的宽度
		imageView7.setFitHeight(20); // 设置图标的高度
		labelPrintingButton.setGraphic(imageView7);

		Image image8 = new Image("icon/logout.png");
		ImageView imageView8 = new ImageView(image8);
		imageView8.setFitWidth(20); // 设置图标的宽度
		imageView8.setFitHeight(20); // 设置图标的高度
		closeButton.setGraphic(imageView8);

		anchorPaneCom.getStylesheets().add(getClass().getResource("/css/system-homepage-background-image.css").toExternalForm());

		helpMenuItem.setOnAction(event -> handleHelp());
	}

	@FXML
	private void handleCodeQuery() {
		FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/StckListReportView.fxml"));
		Stage stage = new Stage();
		Scene scene;
		try {
			scene = new Scene(loader.load());
			stage.setScene(scene);
			stage.setResizable(true);
			stage.setTitle(codeQueryButton.getText());
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@FXML
	private void handleSummaryQuery() {
		System.out.println("点击了汇总查询");

		FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/SummaryQueryView.fxml"));
		loader.setControllerFactory(new Callback<Class<?>, Object>() {
			@Override
			public Object call(Class<?> param) {
				// 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
				// 解决方案：
				// 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
				// 2、@Autowired private ApplicationContext applicationContext;
				// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
				return SpringbootJavafxApplication.applicationContext.getBean(param);
			}
		});
		Stage stage = new Stage();
		Scene scene;
		try {
			scene = new Scene(loader.load());
			stage.setScene(scene);
			stage.setResizable(true);
            stage.setTitle(summaryQueryButton.getText());
			stage.show();
			System.out.println("��������");
		} catch (IOException e) {
			e.printStackTrace();
		}


	}
    @FXML
    private void handleDataAcq() {
        // �����뵥��ѯ�߼�
        System.out.println("点击了数据采集");

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/DataAcqView.fxml"));
        loader.setControllerFactory(new Callback<Class<?>, Object>() {
            @Override
            public Object call(Class<?> param) {
                // 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
                // 解决方案：
                // 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
                // 2、@Autowired private ApplicationContext applicationContext;
                // Object bean_name = applicationContext.getBean("bean Name", Bean.class);
                return SpringbootJavafxApplication.applicationContext.getBean(param);
            }
        });
        Stage stage = new Stage();
        Scene scene;
        try {
            scene = new Scene(loader.load());
            stage.setScene(scene);
            stage.setResizable(true);
//            stage.setTitle(productionReportMenuItem.getText());
            stage.setTitle("数据采集窗口");
            stage.show();
            // �������������߼�
            System.out.println("��������");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	@FXML
	private void handleFinishedProductQuery() {
	}

	@FXML
	private void handleComprehensiveQuery() {
		System.out.println("handleComprehensiveQuery");
	}

	@FXML
	private Button productSettingsButton;
	@FXML
	private Button dataAcqButton;

	@FXML
	private Button productionReportButton;


	@FXML
	private Button codeModificationButton;

	@FXML
	private Button codeQueryButton;

	@FXML
	private Button summaryQueryButton;

	@FXML
	private Button boxSettingsButton;

	@FXML
	private Button codeSettingsButton;

	@FXML
	private void handleScaleSettings() {
		System.out.println("handleScaleSettings");
		FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/ScaleSettingsView.fxml"));
		loader.setControllerFactory(new Callback<Class<?>, Object>() {
			@Override
			public Object call(Class<?> param) {
				// 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
				// 解决方案：
				// 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
				// 2、@Autowired private ApplicationContext applicationContext;
				// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
				return applicationContext.getBean(param);
			}
		});
		Stage stage = new Stage();
		Scene scene;
		try {
			scene = new Scene(loader.load());
			stage.setScene(scene);
			stage.setResizable(true);
			//stage.setTitle(scaleSettingMenuItem.getText());
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void handleProductSettings() {
		FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/product/EntryView.fxml"));
		loader.setControllerFactory(new Callback<Class<?>, Object>() {
			@Override
			public Object call(Class<?> param) {
				// 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
				// 解决方案：
				// 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
				// 2、@Autowired private ApplicationContext applicationContext;
				// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
				return applicationContext.getBean(param);
			}
		});
		Stage stage = new Stage();
		Scene scene;
		try {
			scene = new Scene(loader.load());
			stage.setScene(scene);
			stage.setResizable(true);
			stage.setTitle(productSettingsButton.getText());
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void handleCodeModification() {
		FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/StackListView.fxml"));
		Stage stage = new Stage();
		Scene scene;
		try {
			scene = new Scene(loader.load());
			stage.setScene(scene);
			stage.setResizable(true);
			stage.setTitle(codeModificationButton.getText());
			stage.show();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void handleInitialSetup() {
		try {
			FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/InitialSetupView.fxml"));
			loader.setControllerFactory(new Callback<Class<?>, Object>() {
				@Override
				public Object call(Class<?> param) {
					// 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
					// 解决方案：
					// 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
					// 2、@Autowired private ApplicationContext applicationContext;
					// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
					return applicationContext.getBean(param);
				}
			});
			Stage stage = new Stage();
			Parent root= loader.load();
			Scene scene = new Scene(root);
			stage.setScene(scene);

			stage.setTitle(initialSetupMenuItem.getText());
			stage.show();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

//	@FXML
//	private void handleBoxSettings() {
//		try {
//			FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/BoxSettingView.fxml"));
//			Stage stage = new Stage();
//			Parent root= loader.load();
//			Scene scene = new Scene(root);
//			stage.setScene(scene);
//			stage.setTitle(boxSettingsButton.getText());
//			stage.show();
//			System.out.println("箱单打印设置");
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

//	@FXML
//	private void handleCodeSettings() {
//		FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/CodeListPrintView.fxml"));
//		loader.setControllerFactory(new Callback<>() {
//			@Override
//			public Object call(Class<?> param) {
//				// 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
//				// 解决方案：
//				// 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
//				// 2、@Autowired private ApplicationContext applicationContext;
//				// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
//				CodeListPrintController bean = (CodeListPrintController) applicationContext.getBean(param);
//				bean.produceService = applicationContext.getBean(ScpProduceService.class);
//				bean.scpDataAcqService = applicationContext.getBean(ScpDataAcqService.class);
//				bean.scpCodeFormService = applicationContext.getBean(ScpCodeFormService.class);
//				return bean;
//			}
//		});
//		Stage stage = new Stage();
//		Scene scene;
//		try {
//			scene = new Scene(loader.load());
//			stage.setScene(scene);
//			stage.setResizable(true);
//			stage.setTitle("码单打印设置");
//			stage.show();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//
//	}

	@FXML
	private void handleProductionReport() {
		FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/ReportView.fxml"));
		loader.setControllerFactory(new Callback<Class<?>, Object>() {
			@Override
			public Object call(Class<?> param) {
				// 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
				// 解决方案：
				// 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
				// 2、@Autowired private ApplicationContext applicationContext;
				// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
				return applicationContext.getBean(param);
			}
		});
		Stage stage = new Stage();
		Scene scene;
		try {
			scene = new Scene(loader.load());
			stage.setScene(scene);
			stage.setResizable(true);
			stage.setTitle(productionReportMenuItem.getText());
			stage.show();
			// �������������߼�
			System.out.println("生产报表");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void handleDailyProductionReport() {
	}

	@FXML
	private void handleMonthlyProductionReport() {
	}

	@FXML
	private void handleSplitCodePrint() {
	}

	@FXML
	private void handleExit(ActionEvent event) {
		System.exit(0);
	}

	@FXML
	private void handleSettings(ActionEvent event) {
	}

	@FXML
	private void handleAbout(ActionEvent event) {
	}

	@FXML
	private void handleTaskProcessing(ActionEvent event) {
	}

	@FXML
	private void handleDataQuery(ActionEvent event) {
	}

	@FXML
	private void handleSystemSettings(ActionEvent event) {
	}

	@FXML
	private void loadchangePassword() {
		try {
			FXMLLoader loader = new FXMLLoader(
					getClass().getResource("/fxml/ChangePasswordView.fxml"));
			loader.setControllerFactory(new Callback<Class<?>, Object>() {
				@Override
				public Object call(Class<?> param) {
					// 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
					// 解决方案：
					// 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
					// 2、@Autowired private ApplicationContext applicationContext;
					// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
					return SpringbootJavafxApplication.applicationContext.getBean(param);
				}
			});
			Stage stage = new Stage();
			Scene scene = new Scene(loader.load());
			stage.setScene(scene);
			stage.setResizable(false);
			stage.setTitle("修改密码");
			stage.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void handlePrintSetting() {
		try {
			FXMLLoader loader = new FXMLLoader(
					getClass().getResource("/fxml/PrintSettingView.fxml"));
			loader.setControllerFactory(new Callback<Class<?>, Object>() {
				@Override
				public Object call(Class<?> param) {
					// 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
					// 解决方案：
					// 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
					// 2、@Autowired private ApplicationContext applicationContext;
					// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
					return SpringbootJavafxApplication.applicationContext.getBean(param);
				}
			});
			Stage stage = new Stage();
			Scene scene = new Scene(loader.load());
			stage.setScene(scene);
			stage.setResizable(false);
			stage.setTitle("打印机设置");
			stage.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void handleReportParamSetting() {
		try {
			FXMLLoader loader = new FXMLLoader(
					getClass().getResource("/fxml/ReportParamSettingView.fxml"));
			Stage stage = new Stage();
			Scene scene = new Scene(loader.load());
			stage.setScene(scene);
			stage.setResizable(false);
			stage.setTitle("报表参数设置");
			stage.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void handleLabelPrinting() {
		try {
			FXMLLoader loader = new FXMLLoader(
					getClass().getResource("/fxml/LabelPrintingView.fxml"));
			loader.setControllerFactory(new Callback<Class<?>, Object>() {
				@Override
				public Object call(Class<?> param) {
					// 控制器工厂提供bean注入，此处的缺点是不能根据bean名字注入，只能通过class类型注入bean
					// 解决方案：
					// 1、SpringbootJavafxApplication.applicationContext.getBean("Bean Name", Bean.class);
					// 2、@Autowired private ApplicationContext applicationContext;
					// Object bean_name = applicationContext.getBean("bean Name", Bean.class);
					return SpringbootJavafxApplication.applicationContext.getBean(param);
				}
			});
			Stage stage = new Stage();
			Scene scene = new Scene(loader.load());
			smecta.client.print.controller.LabelPrintController controller = (smecta.client.print.controller.LabelPrintController) loader.getController();

			stage.setScene(scene);
			scene.getStylesheets().add(
					getClass().getResource("/fxml/LabelPrintingViewStyle.css").toExternalForm());
			stage.setResizable(true);
			stage.setTitle("亿维通标签打印管理软件");
			stage.show();
			stage.setOnCloseRequest(new EventHandler<WindowEvent>() {

				@Override
				public void handle(WindowEvent event) {
					TableUtil.saveTableColumnsConfig(controller.getDataTableView());
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void handleHelp() {
		String helpUrl = "https://www.baidu.com/";
		try {
			SpringbootJavafxApplication.hostServices.showDocument(helpUrl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@FXML
	private void handleClose() {
		// 关闭窗口
		Stage stage = (Stage) closeButton.getScene().getWindow();
		stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			@Override
			public void handle(WindowEvent event) {
				// 可以在这里添加关闭前的清理工作，例如保存设置、释放资源等

				// 关闭窗口并退出应用程序
				stage.close();
				System.exit(0);
			}
		});
		stage.close();
	}

}
